package com.easyticket.mail.service.impl;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;

public class TestJavaMailImpl {

    private JavaMailSender mailSender;
    
    private SimpleMailMessage message;
        
    public void sendMail(final String email) {
        
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
                mimeMessage.setFrom(new InternetAddress(message.getFrom()));
                mimeMessage.setSubject(message.getSubject(),"gbk");
                mimeMessage.setText(message.getText(),"gbk");
            }
        };
        mailSender.send(preparator);
    
    }
    
    /**
     * 初步测试javamail
     */
    @Test
    public void send() {
        ApplicationContext context= null;
        context = new ClassPathXmlApplicationContext("classpath:com/easyticket/mail/applicationContext-mail.xml");
        //context = new FileSystemXmlApplicationContext("applicationContext-mail.xml");
        TestJavaMailImpl sms = (TestJavaMailImpl)context.getBean("sendmail");
        sms.sendMail("67104546@qq.com");
    }
    
    
    public JavaMailSender getMailSender() {
        return mailSender;
    }

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public SimpleMailMessage getMessage() {
        return message;
    }

    public void setMessage(SimpleMailMessage message) {
        this.message = message;
    }

}
